<?php

/* 
 * Copyright 2015 gg.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

namespace Com\Wechat;
/**
 * Curl连接类
 */
class CurlModel {
    
    /**
     * curl http下载文件
     * @param type $url 下载url
     * @param type $path 文件存储位置
     * @param type $filename 文件名
     * @param type $ext 文件扩展名
     * @return boolean 是否成功
     */
    public function curlFileGet($url,$path,$filename,$ext){
        $fp = fopen($path.$filename.".".$ext,"w");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt( $ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_FILE, $fp);
        return curl_exec($ch);
    }
    
    
    /**
     * curl https下载文件
     * @param type $url 下载url
     * @param type $path 文件存储位置
     * @param type $filename 文件名
     * @param type $ext 文件扩展名
     * @return boolean 是否成功
     */
    public function curlSafeFileGet($url,$path,$filename,$ext){
        $fp = fopen($path.$filename.".".$ext,"w");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt( $ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_FILE, $fp);
        return curl_exec($ch);
    }

    
    
    /**
     * curl http下载文件
     * @param type $url 下载url
     * @param type $path 文件存储位置
     * @param type $filename 文件名
     * @param type $ext 文件扩展名
     * @param type $postData 传入格式化好的json字符串或者xml字符串
     * @return boolean 是否成功
     */
    public function curlFilePost($url,$path,$filename,$ext,$postData){
        $fp = fopen($path.$filename.".".$ext,"w");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt( $ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        $result = curl_exec($ch);
        fclose($fp);
        return $result;
    }
    
    
    /**
     * curl https下载文件
     * @param type $url 下载url
     * @param type $path 文件存储位置
     * @param type $filename 文件名
     * @param type $ext 文件扩展名
     * @return boolean 是否成功
     */
    public function curlSafeFilePost($url,$path,$filename,$ext,$postData){
        $fp = fopen($path.$filename.".".$ext,"w");
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt( $ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_FILE, $fp);
        return curl_exec($ch);
    }

    /**
     * https post获取内容方法
     * @param type $url
     * @param type $postData 传入格式化好的json字符串或者xml字符串
     * @return bool
     */
    public function curlSafePost($url,$postData){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt( $ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch) ;
        return $result;
    }
    
    
    /**
     * https GET 获取内容方法
     * @param type $url
     * @return type
     */
    public function curlSafeGet($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt( $ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch) ;
        return $result;
    }
    
    
    /**
     * http post获取内容方法
     * @param type $url
     * @param type $postData 传入格式化好的json字符串或者xml字符串
     * @return bool
     */
    public function curlPost($url,$postData){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt( $ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        $result = curl_exec($ch);
        curl_close($ch) ;
        return $result;
    }
    
    
    /**
     * http GET 获取内容方法
     * @param type $url
     * @return type
     */
    public function curlGet($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt( $ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        $result = curl_exec($ch);
        curl_close($ch) ;
        return $result;
    }
    
    
    
    
}
